package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.ArrayList;

import domain.Lyric;

/**
 * Created by public1 on 2017/1/12.
 */

public class ShowLyricView extends TextView {

    private ArrayList<Lyric> lyrics;//歌词列表
    private Paint paint;
    private Paint whitePaint;

    private int width;
    private int height;
    private int currentIndex;//歌词列表索引
    private float textHeight = 60;//每句歌词的高

    private int currentPosition;//当前播放进度
    private float sleepTime;//歌词高亮时间
    private float timePoint;//时间戳，什么时刻到高亮（什么时候唱）

    public void setLyrics(ArrayList<Lyric> lyrics) {
        this.lyrics = lyrics;
        //重绘制
        invalidate();//在主线程
    }

    public ShowLyricView(Context context) {
        this(context,null);
    }

    public ShowLyricView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShowLyricView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = w;
        height = h;
        initView();
    }

    private void initView() {
        //
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(35);
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);//设置居中对齐
        //
        whitePaint = new Paint();
        whitePaint.setColor(Color.WHITE);
        whitePaint.setTextSize(35);
        whitePaint.setAntiAlias(true);
        whitePaint.setTextAlign(Paint.Align.CENTER);//设置居中对齐

        //
        lyrics = new ArrayList<>();
        for (int i = 0;i < 500;i++){
            Lyric lyric = new Lyric();
            lyric.setTimePoint(1000*i);
            lyric.setSleepTime(1500+i);
            lyric.setContent(i+"歌词歌词歌词歌词歌词歌词歌词"+i);
            //
            lyrics.add(lyric);
        }
        

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (lyrics != null && lyrics.size() > 0) {

            //歌词慢慢往上推移
            float plush = 0;
            if (sleepTime == 0){
                plush = 0;
            } else {
                float delta = ((currentPosition - timePoint)/sleepTime) * textHeight;

                plush = textHeight + delta;
            }
            canvas.translate(0,-plush);

            //绘制歌词
            //1.绘制当前句
            String currentText = lyrics.get(currentIndex).getContent();
            canvas.drawText(currentText,width/2,height/2,paint);
            //2.绘制前面部分
            float tempY = height/2;//Y轴中间坐标
            for (int i = currentIndex - 1 ; i >= 0; i--){
                String preContent = lyrics.get(i).getContent();
                tempY = tempY - textHeight;
                if (tempY < 0) {
                    break;
                }
                canvas.drawText(preContent,width/2,tempY,whitePaint);
            }
            //3.绘制下面部分
            tempY = height/2;//Y轴中间坐标
            for (int i = currentIndex + 1 ; i < lyrics.size(); i++){
                String nextContent = lyrics.get(i).getContent();
                tempY = tempY + textHeight;
                if (tempY > height) {
                    break;
                }
                canvas.drawText(nextContent,width/2,tempY,whitePaint);
            }
            
        } else {
            canvas.drawText("暂无歌词...",width/2,height/2,paint);
        }
    }

    /**根据当前播放位置，找出高亮歌词
     * @param currentPosition
     */
    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;

        if (lyrics != null && lyrics.size() > 0) {

            for (int i = 1;i < lyrics.size();i++) {
                if (currentPosition < lyrics.get(i).getTimePoint()){
                    int tempIndex = i - 1;
                    if (currentPosition >= lyrics.get(tempIndex).getTimePoint()) {
                        //当前正在播放的歌词
                        currentIndex = tempIndex;
                        sleepTime = lyrics.get(currentIndex).getSleepTime();
                        timePoint = lyrics.get(currentIndex).getTimePoint();
                    }
                }

            }

            //重绘制
            invalidate();//在主线程

        }
    }
}
